<?php
/**
 * @title 5516. 警告一小时内使用相同员工卡大于等于三次的人 - 哈希
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $keyName
     * @param String[] $keyTime
     * @return String[]
     */
    function alertNames($keyName, $keyTime) {
        /**
         * @since 2020-10-03 结果集
         */
        $result = [];

        /**
         * @since 2020-10-03 哈希数组
         */
        $hashs = [];

        /**
         * @since 2020-10-03 长度
         */
        $len = count($keyName);

        /**
         * @since 2020-10-03 时间升序排列
         */
        array_multisort($keyName, SORT_ASC, SORT_NUMERIC, $keyTime);
        // return [$keyName, $keyTime];

        /**
         * @since 2020-10-03 迭代
         */
        for ($i=0; $i<$len; $i++){
            $name = $keyName[$i];

            /**
             * @since 2020-10-03 是否已被警告
             */
            if(isset($result[$name])){
            } else {
                /**
                 * @since 2020-10-03 数字化时间
                 */
                $time = $keyTime[$i];
                $timeInt = intval(str_replace(":", "", $time));

                /**
                 * @since 2020-10-03 哈希是否存在
                 */
                if(!isset($hashs[$name])){
                    $hashs[$name][] = $timeInt;
                } else {
                    /**
                     * @since 2020-10-03 从队列头取值
                     */
                    while(true){
                        $stime = array_shift($hashs[$name]);

                        /**
                         * @since 2020-10-03 没有数据
                         */
                        if($stime === null){
                            break;
                        } else {
                            /**
                             * @since 2020-10-03 是否在60分钟内
                             */
                            if($timeInt - $stime <= 100){
                                /**
                                 * @since 2020-10-03 检查数组长度, 是否满足被警告3次
                                 */
                                if(count($hashs[$name]) >= 1){
                                    /**
                                     * @since 2020-10-03 60分钟被警告3次
                                     */
                                    $result[$name] = 1;
                                } else {
                                    /**
                                     * @since 2020-10-03 再重新放回去
                                     */
                                    $hashs[$name] = [$stime];
                                    // array_unshift($hashs[$name], $stime);
                                    break;
                                }
                            } else {
                                /**
                                 * @since 2020-10-03 超出60分钟
                                 */
                            }
                        }
                    }

                    /**
                     * @since 2020-10-03 记录本次记录
                     */
                    $hashs[$name][] = $timeInt;
                }
            }
        }

        /**
         * @since 2020-10-03 提取姓名
         */
        $names = array_keys($result);
        sort($names);

        /**
         * @return
         */
        return $names;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.03_2.php
 */
$datas = [
    // [
    //     ["daniel","daniel","daniel","luis","luis","luis","luis"],
    //     ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"],
    // ],
    // [
    //     ["alice","alice","alice","bob","bob","bob","bob"],
    //     ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
    // ],
    // [
    //     ["john","john","john"],
    //     ["23:58","23:59","00:01"]
    // ],
    [
        ["leslie","leslie","leslie","clare","clare","clare","clare"],
        ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"]
    ],
    [
        ["clare","clare","clare","clare"],
        ["18:00","18:51","19:30","19:49"]
    ],
    // [
    //     ["bob", "jack", "tom"],
    //     ["09:00", "14:00", "12:00"],
    // ]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->alertNames(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
